package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.dto.CheckGroupDTO;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.mapper.CheckGroupMapper;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckGroupService;
import com.itheima.health.service.CheckItemService;
import com.itheima.health.vo.CheckGroupVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

// 2.7.8
@Service
@Transactional
public class CheckGroupServiceImpl extends ServiceImpl<CheckGroupMapper, CheckGroup> implements CheckGroupService {
    @Autowired
    private  CheckItemService checkItemService;
//保存组
    @Override
//    保存或者编辑组
    public boolean saveOrUpdateGroup(CheckGroupDTO checkGroupDTO) {
        Integer  gid = checkGroupDTO.getId();
        if(gid!=null){
            //  update       //  删除 。。。。中间表记录  delete  from  xxx where gid = ?
            baseMapper.deleteGroupAndCheckItemInfoByGid(gid);
        }
        //  多表更新   checkGroupDTO  存在id  update
        boolean flag =   saveOrUpdate(checkGroupDTO);//  自动回显  id
        gid = checkGroupDTO.getId();
        //  中间表更新
        Integer[] checkitemIds = checkGroupDTO.getCheckitemIds();
        if(checkitemIds!=null){
            for (Integer checkitemId : checkitemIds) {
                baseMapper.addGroupInfoAndCheckItemInfo(gid,checkitemId);
            }
        }
        return flag;
    }


    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Page pageResult = null;
        //判断是否为空
        QueryWrapper<CheckGroup> queryWrapper = new QueryWrapper<CheckGroup>();
        queryWrapper.eq("is_delete",0);//必须是有效的数据
        if (StringUtils.isNotBlank(queryPageBean.getQueryString())) {
            queryWrapper.like("HELPCODE", queryPageBean.getQueryString());
            pageResult = page(new Page<CheckGroup>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()),queryWrapper);
        } else {
//            无条件的任务查询 业务层实现
            pageResult = page(new Page<CheckGroup>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()),queryWrapper);
        }
        return new PageResult(pageResult.getTotal(), pageResult.getRecords());
    }

    @Override
    public CheckGroupVO findGroupInfoById(int id) {
        List<CheckItem> list = checkItemService.list();
        int[] checkItemIds =  baseMapper.findCheckItemIdsByGroupId(id);
        CheckGroupVO checkGroupVO = new CheckGroupVO();
        checkGroupVO.setCheckItems(list);
        checkGroupVO.setCheckItemIds(checkItemIds);
        return checkGroupVO;
    }

    @Override
    public List<CheckGroupVO> findCheckGroupInfoBySetmealId(int sid) {

        return  baseMapper.findCheckGroupInfoBySetmealId(sid);
    }
}
